﻿using System;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Data;
using GSSoftSL.Models;
using GSSoftSL.Utility;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Windows.Data.DomainServices;

namespace GSSoftSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class GiaoVienViewModel : ViewModelBase
    {
        private bool _isBusy = false;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        public int[] Items
        {
            get { return new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; }
        }

        /*private int _value;

        public int CurrentInt
        {
            get { return _value; }
            set
            {
                _value = value;
                RaisePropertyChanged("CurrentInt");
            }
        }*/

        private CheckBoxContext[] _mons;
        public CheckBoxContext[] Mons
        {
            get { return _mons; }
            set 
            { 
                _mons = value;
                RaisePropertyChanged("Mons");
            }
        }

        private GSSoftContext _dc;

        /// <summary>
        /// Initializes a new instance of the GiaoVienViewModel class.
        /// </summary>
        public GiaoVienViewModel()
        {
            Init();
        }

        private bool _isLoadingGiaoVien = false;
        private bool _isLoadingMon = false;
        private void Init()
        {
            _dc = new GSSoftContext();//(GSSoftContext);context;
            IsBusy = _isLoadingGiaoVien = _isLoadingMon = true;
            _dc.Load<Mon>(_dc.GetMonsQuery().OrderBy(n => n.ThuTu), LoadBehavior.RefreshCurrent, LoadMon_Completed, null);
        }

        private void LoadGiaoVienCompleted(LoadOperation<GiaoVien> load)
        {
            GiaoViens = new PagedCollectionView(new EntityList<GiaoVien>(_dc.GiaoViens, load.Entities));
            _isLoadingGiaoVien = false;
            //IsBusy = _isLoadingGiaoVien || _isLoadingMon;
            IsBusy = false;
        }

        private void LoadMon_Completed(LoadOperation<Mon> load)
        {
            Mons =
               load.Entities.Select(n => new CheckBoxContext() { Key = n.MaMon, Text = n.TenMon, IsChecked = false }).ToArray();
            //RaisePropertyChanged("Mons");
            _isLoadingMon = false;
            _dc.Load<GiaoVien>(_dc.GetGiaoViensWithDetailsQuery().OrderBy(n => n.MaGV), LoadBehavior.RefreshCurrent, LoadGiaoVienCompleted, null);
            //IsBusy = _isLoadingGiaoVien || _isLoadingMon;
        }

        private void CompletedMons(object sender, EventArgs e)
        {
            LoadOperation<Mon> loadMons = sender as LoadOperation<Mon>;
            Mons =
                loadMons.Entities.Select(n => new CheckBoxContext() {Key = n.MaMon, Text = n.TenMon, IsChecked = false})
                    .ToArray();
            RaisePropertyChanged("Mons");
            //throw new NotImplementedException();
        }
        
        private PagedCollectionView _giaoViens = null;
        public PagedCollectionView GiaoViens
        {
            get
            {
                return _giaoViens;
            }
            set
            {
                if (_giaoViens == value)
                {
                    return;
                }
                _giaoViens = value;
                RaisePropertyChanged("GiaoViens");
            }
        }
        //public PagedCollectionView GiaoViens { get; set; }

        private GiaoVien _current;
        public GiaoVien Current
        {
            get { return _current; } 
            set 
            { 
                _current = value;
                if (_current != null && _current.MaGV == null)
                {
                    _current.MaGV = "";
                    DayBirth = MonthBirth = 1;
                    YearBirth = 1900;
                }
                else if (_current != null)
                {
                    if (_current.NgaySinh.HasValue)
                    {
                        DayBirth = _current.NgaySinh.Value.Day;
                        MonthBirth = _current.NgaySinh.Value.Month;
                        YearBirth = _current.NgaySinh.Value.Year;
                    }
                    else
                    {
                        DayBirth = MonthBirth = 1;
                        YearBirth = 1900;
                    }
                }
                RaisePropertyChanged("Current");
            }
        }

        private RelayCommand _saveCommand;

        /// <summary>
        /// Gets the SaveCommand.
        /// </summary>
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand
                       ?? (_saveCommand = new RelayCommand(
                                              () =>
                                                  {
                                                      if (Current == null || !_dc.HasChanges || _dc.IsSubmitting)
                                                          return;
                                                      ConfirmWindow.CreateNew("Bạn có muốn lưu?", Save);
                                                  },
                                              () => !(Current == null || !_dc.HasChanges || _dc.IsSubmitting)));
            }
        }

        private void Save()
        {
            if (Current.EntityState == EntityState.New)
            {
                Current.NgayTao = Current.NgayCapNhat = DateTime.Now;
                Current.NguoiTao = Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            else if (Current.EntityState == EntityState.Modified)
            {
                Current.NgayCapNhat = DateTime.Now;
                Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }

            IsBusy = true;
            _dc.SubmitChanges(SubmitChangesEnd, null);
        }

        //public RelayCommand<object> SaveCommand { get; private set; }

        private void SaveChanges(object obj)
        {
            if (Current == null || !_dc.HasChanges || _dc.IsSubmitting)
                return;
            if (Current.EntityState == EntityState.New)
            {
                Current.NgayTao = Current.NgayCapNhat = DateTime.Now;
                Current.NguoiTao = Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            else if (Current.EntityState == EntityState.Modified)
            {
                Current.NgayCapNhat = DateTime.Now;
                Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
                
            }

            IsBusy = true;
            _dc.SubmitChanges(SubmitChangesEnd, null);
        }

        private void SubmitChangesEnd(SubmitOperation obj)
        {
            IsBusy = false;
        }

        private RelayCommand _refreshCommand;
        public RelayCommand RefreshCommand
        {
            get
            {
                return _refreshCommand
                    ?? (_refreshCommand = new RelayCommand(Init));
            }
        }

        
        private int _dayBirth = 1;
        public int DayBirth
        {
            get
            {
                return _dayBirth;
            }
            set
            {
                if (_dayBirth == value)
                {
                    return;
                }
                _dayBirth = value;
                RaisePropertyChanged("DayBirth");
            }
        }

        
        private int _monthBirth = 1;
        public int MonthBirth
        {
            get
            {
                return _monthBirth;
            }
            set
            {
                if (_monthBirth == value)
                {
                    return;
                }
                _monthBirth = value;
                RaisePropertyChanged("MonthBirth");
            }
        }

        
        private int _yearBirth = 1900;
        public int YearBirth
        {
            get
            {
                return _yearBirth;
            }
            set
            {
                if (_yearBirth == value)
                {
                    return;
                }
                _yearBirth = value;
                RaisePropertyChanged("YearBirth");
            }
        }

        #region CheckBoxList



        #endregion
    }
}